package com.lagou.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Roger
 * @date 2020/8/15 9:17
 * @description
 */
@Component()
public class TransactionManager{

    @Autowired
    private ConnectionUtil connectionUtil;


    public void startTransaction(){
        System.out.println("123");
        Connection connection = connectionUtil.getConnection();
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void commit(){
        try {
            connectionUtil.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void rollBack(){
        try {
            connectionUtil.getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void release(){
        try {
            connectionUtil.getConnection().setAutoCommit(true);
            connectionUtil.getConnection().close();
            connectionUtil.removeThreadConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
